//
//  UnincorporateVM.swift
//  kxf
//
//  Created by Elavatine on 2024/10/8.
//


import Foundation
import UIKit
import MCToast

class UnincorporateVM: UIView {
    
    var selfHeight = UnincorporatePhotoVM().selfHeight + UnincorporateMsgVM().selfHeight
    
    var idFrontMsgDict = NSDictionary()
    var idBackMsgDict = NSDictionary()
    
    var nextBlock:(()->())?
    
    override init(frame:CGRect){
        super.init(frame: CGRect.init(x: 0, y: frame.origin.y, width: SCREEN_WIDHT, height: selfHeight))
//        self.selfHeight = frame.size.height
        self.backgroundColor = .COLOR_LIGHT_GREY
        self.isUserInteractionEnabled = true
        
        initUI()
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    lazy var photoVm : UnincorporatePhotoVM = {
        let vm = UnincorporatePhotoVM.init(frame: CGRect.init(x: 0, y: 0, width: 0, height: 0))
        vm.idOcrFrontBlock = {(dict)in
            self.idFrontMsgDict = dict
            self.updateIdCardFrontMsg()
        }
        vm.idOcrBackBlock = {(dict)in
            self.idBackMsgDict = dict
            self.udpateIdCardBackMsg()
        }
        return vm
    }()
    lazy var msgVm : UnincorporateMsgVM = {
        let vm = UnincorporateMsgVM.init(frame: CGRect.init(x: 0, y: self.photoVm.frame.maxY, width: 0, height: 0))
        
        return vm
    }()
}


extension UnincorporateVM{
    func initUI(){
        addSubview(photoVm)
        addSubview(msgVm)
    }
    func updateUIForRefuse() {
        photoVm.updateUIForRefuse()
        msgVm.updateUIForRefuse()
//        bankNoVm.textField.text = AuthenCompanyMsgModel.shareInstance.settleBankNo
//        bankTypeVm.textField.text = AuthenCompanyMsgModel.shareInstance.settleBankTypeName
//        legalTypeVm.textField.text = AuthenCompanyMsgModel.shareInstance.settleWayName
//        bankPrivateNameVm.textField.text = AuthenCompanyMsgModel.shareInstance.settleBankNamePrivate
//        bankPrivateBranchVm.setLabelContent(content: "\(AuthenCompanyMsgModel.shareInstance.settleBankBranchNamePrivate)")
//        bankPhoneVm.textField.text = AuthenCompanyMsgModel.shareInstance.settleBankPhone
//        bankPublicNoVm.textField.text = AuthenCompanyMsgModel.shareInstance.settleBankNoPublic
//        bankPublicNameVm.textField.text = AuthenCompanyMsgModel.shareInstance.settleBankNamePublic
//        bankPublicBranchVm.setLabelContent(content: "\(AuthenCompanyMsgModel.shareInstance.settleBankBranchNamePublic)")
    }
    func updateIdCardFrontMsg() {
        msgVm.idNameVm.textField.text = self.idFrontMsgDict["name"]as? String ?? ""
        msgVm.idNoVm.textField.text = self.idFrontMsgDict["code"]as? String ?? ""
//        msgVm.idAddressVm.textField.text = self.idFrontMsgDict["address"]as? String ?? ""
    }
    func udpateIdCardBackMsg() {
        msgVm.idStartTimeVm.setLabelContent(content: Date().changeDateFormatter(dateString: "\(idBackMsgDict["issueDate"]as? String ?? "")"))
        msgVm.idEndTimeVm.setLabelContent(content: Date().changeDateFormatter(dateString: "\(idBackMsgDict["expiryDate"]as? String ?? "")"))
    }
    func getValue() {
        AuthenCompanyMsgModel.shareInstance.unincorporateIdCardName = msgVm.idNameVm.textField.text ?? ""
        AuthenCompanyMsgModel.shareInstance.unincorporateIdCardNo = msgVm.idNoVm.textField.text ?? ""
        AuthenCompanyMsgModel.shareInstance.unincorporateIdCardStartDate = msgVm.idStartTimeVm.rightLabel.text ?? ""
        AuthenCompanyMsgModel.shareInstance.unincorporateIdCardEndDate = msgVm.idEndTimeVm.rightLabel.text ?? ""
        AuthenCompanyMsgModel.shareInstance.unincorporateBankTel = msgVm.bankPhoneVm.textField.text ?? ""
//        AuthenCompanyMsgModel.shareInstance.unincorporateBankName = msgVm.bankPrivateNameVm.textField.text ?? ""
        AuthenCompanyMsgModel.shareInstance.unincorporateCardNo = msgVm.bankNoVm.textField.text ?? ""
        
    }
    func checkValue() -> Bool {
        if AuthenCompanyMsgModel.shareInstance.settleWay == "LEGAL_PERSON"{
            return true
        }
        getValue()
        if AuthenCompanyMsgModel.shareInstance.unincorporateIdCardFrontImage.count < 3{
            MCToast.mc_text("请上传非法人身份证人像面")
            return false
        }
        if AuthenCompanyMsgModel.shareInstance.unincorporateIdCardBackImage.count < 3{
            MCToast.mc_text("请上传非法人身份证国徽面")
            return false
        }
        if AuthenCompanyMsgModel.shareInstance.unincorporateHandheldIdCard.count < 3{
            MCToast.mc_text("请上传非法人手持身份证照")
            return false
        }
//        if AuthenCompanyMsgModel.shareInstance.unincorporateBankCardImage.count < 3{
//            MCToast.mc_text("请上传非法人银行卡照")
//            return false
//        }
        if AuthenCompanyMsgModel.shareInstance.unincorpoProofMchntImg.count < 3{
            MCToast.mc_text("请上传非法人结算证明")
            return false
        }
        if AuthenCompanyMsgModel.shareInstance.unincorporateIdCardName.count < 1{
            MCToast.mc_text("请输入非法人姓名")
            return false
        }
        if AuthenCompanyMsgModel.shareInstance.unincorporateIdCardNo.count < 8{
            MCToast.mc_text("请输入非法人证件号")
            return false
        }
        if AuthenCompanyMsgModel.shareInstance.unincorporateIdCardStartDate.count < 3{
            MCToast.mc_text("请选择法人证件有效期始")
            return false
        }
        if AuthenCompanyMsgModel.shareInstance.unincorporateIdCardEndDate.count < 3{
            MCToast.mc_text("请选择法人证件有效期至")
            return false
        }
//        if AuthenCompanyMsgModel.shareInstance.legalAddress.count < 4{
//            MCToast.mc_text("请输入法人身份证地址")
//            return false
//        }
        if AuthenCompanyMsgModel.shareInstance.unincorporateCardNo.count < 5 {
            MCToast.mc_text("请输入非法人银行卡号")
            return false
        }
//        if AuthenCompanyMsgModel.shareInstance.unincorporateBank.count < 2 {
//            MCToast.mc_text("请输入非法人结算银行")
//            return false
//        }
        if AuthenCompanyMsgModel.shareInstance.unincorporateBankName.count < 2 {
            MCToast.mc_text("请选择非法人开户支行")
            return false
        }
//        if AuthenCompanyMsgModel.shareInstance.settleBankNamePrivate.count < 2 {
//            MCToast.mc_text("请输入对私银行名称")
//            return false
//        }
//        if AuthenCompanyMsgModel.shareInstance.settleBankBranchNamePrivate.count < 2 {
//            MCToast.mc_text("请选择对私开户支行")
//            return false
//        }
        if !WHBaseViewVC().judgePhoneNumber(phoneNum: AuthenCompanyMsgModel.shareInstance.unincorporateBankTel){
            MCToast.mc_text("请输入正确的非法人预留手机号")
            return false
        }
        return true
    }
}
